package com.example.demo.util;

import org.docx4j.Docx4J;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @Author：zhoutaoming
 * @name：DocUtils
 * @Date：2024/7/30 16:30
 * @Version 1.0
 */
public class DocUtils {

    private static void convertDocx2Html(String templatePath) throws Exception {



        File templateFile = new File(templatePath);

        String name = "1.html";

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(templateFile);

        HTMLSettings htmlSettings = Docx4J.createHTMLSettings();

        String folder = templateFile.getParent() + "/out/";

        htmlSettings.setImageDirPath(folder + name + "_files");//生成的html文件与图片文件夹之类的放置同一个目录下

        htmlSettings.setImageTargetUri(name + "_files");

        htmlSettings.setWmlPackage(wordMLPackage);

        File outFile = new File(folder , name);

        Docx4J.toHTML(htmlSettings, new FileOutputStream(outFile), Docx4J.FLAG_NONE);

    }
    public static void main(String[] args) throws Exception {
//        String filePath = "D:\\poi-tl\\管理员授权委托书(社区ID版本）模板.docx";
//        String targetPath =  "D:\\poi-tl\\管理员授权委托书(社区ID版本）.docx";
//        XWPFTemplate template = XWPFTemplate.compile(filePath).render(
//                new HashMap<String, Object>() {
//                    {
//                        put("companyName", "爱问医联科技（深圳）有限公司");
//                        put("userName", "赵四");
//                    }
//                });
//        template.writeAndClose(new FileOutputStream(targetPath));
        DocUtils.convertDocx2Html("D:/docxconvert/1.docx");
    }

}
